﻿Class MainWindow
    Dim Playing, Pausing As Boolean
    Dim BGMPlayer As System.Media.SoundPlayer

    Private Sub SetPlayButtonState(state$)
        Button_Play.Tag = state
        Select Case state
            Case "play"
                Button_Play_Text.Text = "播放"
                Button_Play_Image.Source = New BitmapImage(New Uri("pack://application:,,,/Simple MIVP;component/Resources/control.png"))
            Case "pause"
                Button_Play_Text.Text = "暂停"
                Button_Play_Image.Source = New BitmapImage(New Uri("pack://application:,,,/Simple MIVP;component/Resources/control-pause.png"))
            Case "resume"
                Button_Play_Text.Text = "继续"
                Button_Play_Image.Source = New BitmapImage(New Uri("pack://application:,,,/Simple MIVP;component/Resources/control.png"))
        End Select
    End Sub

    Private Sub TogglePlaying(p As Boolean)
        If p Then
            Playing = True
            Pausing = False
            VideoList.SelectedIndex = 0
            ListControlPanel.IsEnabled = False
            ListPanel.IsEnabled = False
            PlayCurrentVideo()
            SetPlayButtonState("pause")
            If Not IsNothing(BGMPlayer) Then BGMPlayer.PlayLooping()
        Else
            Playing = False
            Pausing = False
            VideoList.SelectedIndex = -1
            ListControlPanel.IsEnabled = True
            ListPanel.IsEnabled = True
            SetPlayButtonState("play")
            If Not IsNothing(BGMPlayer) Then BGMPlayer.Stop()
        End If
    End Sub

    ''' <summary>
    ''' 如果调用此过程时正在播放，则根据 p 的值来决定是暂停(True)还是继续
    ''' (False)；否则引发错误。
    ''' </summary>
    ''' <param name="p">决定是暂停(True)还是继续(False)。</param>
    Private Sub PauseResume(p As Boolean)
        If Not Playing Then
            Throw New Exception("在没有播放的时候调用 PauseResume。")
        End If
        If p Then
            Pausing = True
            PreviewArea.Pause()
            SetPlayButtonState("resume")
            If Not IsNothing(BGMPlayer) Then BGMPlayer.PlayLooping()
        Else
            Pausing = False
            PreviewArea.Play()
            SetPlayButtonState("pause")
            If Not IsNothing(BGMPlayer) Then BGMPlayer.Stop()
        End If
    End Sub

    Private Sub Button_Add_Click(sender As Object, e As RoutedEventArgs) Handles Button_Add.Click
        Dim d As New Microsoft.Win32.OpenFileDialog()
        d.Filter = "视频文件 (自己选，我不知道支持哪些格式……)|*.*"
        If d.ShowDialog() = True Then
            Dim i As New ListBoxItem()
            i.Content = d.FileName
            VideoList.Items.Add(i)
        End If
    End Sub

    Private Sub Button_Remove_Click(sender As Object, e As RoutedEventArgs) Handles Button_Remove.Click
        For i = VideoList.Items.Count - 1 To 0 Step -1
            If VideoList.Items(i).IsSelected Then
                VideoList.Items.RemoveAt(i)
            End If
        Next
    End Sub

    Private Sub Button_Play_Click(sender As Object, e As RoutedEventArgs) Handles Button_Play.Click
        Select Case Button_Play.Tag
            Case "play"
                TogglePlaying(True)
            Case "pause"
                PauseResume(True)
            Case "resume"
                PauseResume(False)
        End Select
    End Sub

    Private Sub Button_SortUp_Click(sender As Object, e As RoutedEventArgs) Handles Button_SortUp.Click

    End Sub

    Private Sub Button_SortDown_Click(sender As Object, e As RoutedEventArgs) Handles Button_SortDown.Click

    End Sub

    Private Sub PreviewArea_MediaEnded(sender As Object, e As RoutedEventArgs) Handles PreviewArea.MediaEnded
        If VideoList.SelectedIndex >= VideoList.Items.Count - 1 Then
            TogglePlaying(False)
        Else
            VideoList.SelectedIndex += 1
            PlayCurrentVideo()
        End If
    End Sub

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)

    End Sub

    Private Sub PlayCurrentVideo()
        PreviewArea.Source = New Uri(VideoList.SelectedItem.Content.ToString)
        PreviewArea.Play()
    End Sub

    Private Sub M_About_Click(sender As Object, e As RoutedEventArgs) Handles M_About.Click
        Dim About As New AboutWindow
        About.ShowDialog()
    End Sub

    Private Sub Button_BGMBrowse_Click(sender As Object, e As RoutedEventArgs) Handles Button_BGMBrowse.Click
        Dim d As New Microsoft.Win32.OpenFileDialog()
        d.Filter = "Wave 波形声音|*.wav"
        If d.ShowDialog() = True Then
            BGMLocation.Text = d.FileName
        End If
    End Sub

    Private Sub Button_BGMClear_Click(sender As Object, e As RoutedEventArgs) Handles Button_BGMClear.Click
        BGMLocation.Text = ""
    End Sub

    Private Sub BGMLocation_LostFocus(sender As Object, e As RoutedEventArgs) Handles BGMLocation.LostFocus
        If BGMLocation.Text <> "" Then
            If Not IsNothing(BGMPlayer) Then BGMPlayer.Dispose()
            BGMPlayer = New System.Media.SoundPlayer(BGMLocation.Text)
        Else
            BGMPlayer = Nothing
        End If
    End Sub

    Private Sub M_Quit_Click(sender As Object, e As RoutedEventArgs) Handles M_Quit.Click
        Close()
    End Sub

    Private Sub UpdateAddRemoveButton()
        Select Case ListPanel.SelectedItem.Tag
            Case "videos"
                Button_Add.IsEnabled = True
                Button_Remove.IsEnabled = True
                SortButtons.IsEnabled = True
                Button_Add_Image.Source = New BitmapImage(New Uri("pack://application:,,,/Simple MIVP;component/Resources/film--plus.png"))
                Button_Remove_Image.Source = New BitmapImage(New Uri("pack://application:,,,/Simple MIVP;component/Resources/film--minus.png"))
            Case "texts"
                Button_Add.IsEnabled = True
                Button_Remove.IsEnabled = True
                SortButtons.IsEnabled = False
                Button_Add_Image.Source = New BitmapImage(New Uri("pack://application:,,,/Simple MIVP;component/Resources/notebook--plus.png"))
                Button_Remove_Image.Source = New BitmapImage(New Uri("pack://application:,,,/Simple MIVP;component/Resources/notebook--minus.png"))
            Case "bgm"
                Button_Add.IsEnabled = False
                Button_Remove.IsEnabled = False
                SortButtons.IsEnabled = False
            Case "sounds"
                Button_Add.IsEnabled = True
                Button_Remove.IsEnabled = True
                SortButtons.IsEnabled = False
                Button_Add_Image.Source = New BitmapImage(New Uri("pack://application:,,,/Simple MIVP;component/Resources/music--plus.png"))
                Button_Remove_Image.Source = New BitmapImage(New Uri("pack://application:,,,/Simple MIVP;component/Resources/music--minus.png"))
        End Select
    End Sub

    Private Sub ListPanel_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ListPanel.SelectionChanged
        UpdateAddRemoveButton()
    End Sub

    Private Sub Window_Main_Loaded(sender As Object, e As RoutedEventArgs) Handles Window_Main.Loaded
        Playing = False
        SetPlayButtonState("play")
        UpdateAddRemoveButton()
    End Sub
End Class
